package com.xtool.livewallpaper

import android.content.Intent
import android.content.res.Configuration
import android.graphics.Typeface
import android.os.Bundle
import android.util.TypedValue
import androidx.appcompat.app.AppCompatDelegate
import androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.gyf.immersionbar.ImmersionBar
import com.xtool.livewallpaper.base.BaseActivity
import com.xtool.livewallpaper.base.BaseFragmentAdapter
import com.xtool.livewallpaper.util.ToastUtil
import com.xtool.livewallpaper.base.getColorCompat
import com.xtool.livewallpaper.databinding.ActivityMainBinding
import com.xtool.livewallpaper.local.LocalFragment
import com.xtool.livewallpaper.local.NetworkFragment

class MainActivity : BaseActivity() {
    private val binding: ActivityMainBinding by lazy { ActivityMainBinding.inflate(layoutInflater) }
    private val adapter: BaseFragmentAdapter<Fragment> by lazy {
        BaseFragmentAdapter(
            listOf(LocalFragment()),
            supportFragmentManager,
            lifecycle
        )
    }
    private var lastPressTime = 0L
    override fun onStart() {
        super.onStart()
        val uiMode = resources.configuration.uiMode
        val isNightMode = AppCompatDelegate.getDefaultNightMode() == MODE_NIGHT_YES
                || uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES
        ImmersionBar.with(this)
            .statusBarDarkFont(!isNightMode)
            .transparentStatusBar()
            .init()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        binding.viewpager.adapter = adapter
        binding.btnSetting.setOnClickListener {
            startActivity(Intent(this, SettingActivity::class.java))
        }
//        binding.tab1.setOnClickListener {
//            binding.viewpager.setCurrentItem(0, true)
//            selectTab1()
//        }
//        binding.tab2.setOnClickListener {
//            binding.viewpager.setCurrentItem(1, true)
//            selectTab2()
//        }
//        binding.viewpager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
//            override fun onPageSelected(position: Int) {
//                super.onPageSelected(position)
//                if (position == 0) {
//                    selectTab1()
//                } else {
//                    selectTab2()
//                }
//            }
//        })
    }

//    private fun selectTab1() {
//        binding.tab1.setTextColor(getColorCompat(R.color.text_color))
//        binding.tab2.setTextColor(getColorCompat(R.color.gray))
//        binding.tab1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18f)
//        binding.tab2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f)
//        binding.tab1.typeface = Typeface.DEFAULT_BOLD
//        binding.tab2.typeface = Typeface.DEFAULT
//    }
//
//    private fun selectTab2() {
//        binding.tab1.setTextColor(getColorCompat(R.color.gray))
//        binding.tab2.setTextColor(getColorCompat(R.color.text_color))
//        binding.tab1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f)
//        binding.tab2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18f)
//        binding.tab1.typeface = Typeface.DEFAULT
//        binding.tab2.typeface = Typeface.DEFAULT_BOLD
//    }

    override fun onBackPressed() {
        if (System.currentTimeMillis() - lastPressTime <= 2000L) {
            super.onBackPressed()
        } else {
            ToastUtil.show(R.string.exit_tips)
            lastPressTime = System.currentTimeMillis()
        }
    }
}